package aar.test.tessocr;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import aar.test.tessocr.base.BaseActivity;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.model.VoiceSetModel;
import aar.test.tessocr.model.loginModel;
import aar.test.tessocr.tessOcrUtil.SwitchButton;
import aar.test.tessocr.utils.Urls;
import clx.tessocr.ocreng.R;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;

import static aar.test.tessocr.LoginActivity.JSON;

public class SettingActivity extends BaseActivity {

    private LinearLayout left_layout;

    private SwitchButton voice_set;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);


        voice_set=findViewById(R.id.voice_set);




        voice_set.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked,int mmm) {


                Log.e("MainActivity","---------------------isChecked=="+isChecked);
                //TODO do your job

                if(mmm==1){

                }else{
                    if(isChecked){
                        voiceSet(1);
                    }else{
                        voiceSet(0);
                    }
                }
                voice_set.setIsfirst(0);

            }
        });




        left_layout=findViewById(R.id.left_layout);
        left_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        voiceSearch();
    }


    private void voiceSearch(){

        Map<String ,Object> map=new HashMap<>();
        map.put("type",1);


        Gson gg=new Gson();
        String json =gg.toJson(map);

        RequestBody body = RequestBody.create(JSON,json);
        OkGo.<LzyResponse<VoiceSetModel>>post(Urls.URL_VOICESET)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<VoiceSetModel>>(SettingActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<VoiceSetModel>> response) {


                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data);
                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data.getStatus());
                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().msg);

                        if(null!=response.body().data&&null!=response.body().data.getStatus()){

                            if(Integer.valueOf(response.body().data.getStatus())==0){
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        voice_set.setChecked(false);
                                        voice_set.setIsfirst(1);
                                    }
                                });


                            }else{

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        voice_set.setChecked(true);
                                        voice_set.setIsfirst(1);
                                    }
                                });



                            }



                        }



                    }

                    @Override
                    public void onError(Response<LzyResponse<VoiceSetModel>> response) {
                        Log.e("MainActivity","onError---------------------");
                    }
                });



    }

    private void voiceSet(int value){

        Map<String ,Object> map=new HashMap<>();
        map.put("type",2);
        map.put("status",value);

        Gson gg=new Gson();
        String json =gg.toJson(map);

        RequestBody body = RequestBody.create(JSON,json);
        OkGo.<LzyResponse>post(Urls.URL_VOICESET)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse>(SettingActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse> response) {


                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data);

                        if(null!=response.body().msg){
                            Toast.makeText(SettingActivity.this,response.body().msg,Toast.LENGTH_LONG).show();
                        }

                    }

                    @Override
                    public void onError(Response<LzyResponse> response) {
                        Log.e("MainActivity","onError---------------------");
                        if(null!=response.body().msg){
                            Toast.makeText(SettingActivity.this,response.body().msg,Toast.LENGTH_LONG).show();
                        }
                    }
                });



    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Activity销毁时，取消网络请求
        OkGo.getInstance().cancelTag(this);

    }

}
